package vis;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;

import logic.GameObject;
import logic.Scene;

public class ViewPanel extends JPanel {
	private static final long serialVersionUID = 1485066833676573821L;
	
	private Color backColor = Color.green;
	private Scene scene = null;
	private final Paint bumpUL = new LinearGradientPaint(0.0f, 0.0f, 100.0f, 100.0f, new float[] {0.0f, 1.0f}, new Color[] {Color.white, Color.green });
	private final Paint bumpUR = new LinearGradientPaint(1000.0f, 0.0f, 900.0f, 100.0f, new float[] {0.0f, 1.0f}, new Color[] {Color.white, Color.green });
	private final Paint bumpDL = new LinearGradientPaint(0.0f, 500.0f, 100.0f, 400.0f, new float[] {0.0f, 1.0f}, new Color[] {Color.white, Color.green });
	private final Paint bumpDR = new LinearGradientPaint(1000.0f, 500.0f, 900.0f, 400.0f, new float[] {0.0f, 1.0f}, new Color[] {Color.white, Color.green });
	
	private final Font scoreFont = new Font(Font.SANS_SERIF, Font.BOLD, 48);
	private final Color scoreColor = new Color(0, 192, 0);
	
	public ViewPanel(Scene scene)
	{
		super(true);
		this.scene = scene;
		this.setMinimumSize(scene.getSize());
		this.setPreferredSize(scene.getSize());
	}
	
	public void paint(Graphics gg)
	{
		Graphics2D g = (Graphics2D)gg;
		g.setBackground(backColor);
		g.clearRect(0, 0, getWidth(), getHeight());
		
		
		g.setFont(scoreFont);
		FontMetrics fm = g.getFontMetrics();
		
		g.setPaint(bumpUL);
		g.fillRect(0, 0, 200, 200);
		g.setPaint(bumpUR);
		g.fillRect(800, 0, 200, 200);
		g.setPaint(bumpDL);
		g.fillRect(0, 300, 200, 200);
		g.setPaint(bumpDR);
		g.fillRect(800, 300, 200, 200);
		
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		g.setColor(scoreColor);
		g.drawLine(500, 50, 500, 450);
		g.drawRect(100, 50, 800, 400);
		g.drawOval(400, 150, 200, 200);

		String text = "";
		if(scene.getResetTimer() == 0)
		{
			g.setColor(scoreColor);
			text = scene.getScore1() + "   " + scene.getScore2();
		}
		else
		{
			g.setColor(Color.YELLOW);
			text = "GOAL  " + scene.getScore1() + "   " + scene.getScore2() + "  GOAL";
		}
		
		Rectangle2D rect = fm.getStringBounds(text, g);
		g.drawString(text, 500 - (int)rect.getWidth() / 2, 250 + (int)rect.getHeight() / 3);
		
		if(scene != null)
		{
			for(GameObject go : scene.getObjects())
				go.draw(g);
		}
	}
}
